package com.voute.Presenter.Impl;

import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.PersonInfoBean;
import com.voute.Model.Bean.SimplePushMessageBean;
import com.voute.Model.IPersonInfoModel;
import com.voute.Model.Impl.IPersonInfoModelImpl;
import com.voute.Presenter.IPersonInfoPresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.IPersonInfoView;

import rx.functions.Action0;

/**
 * 获取自己信息的presenter
 * Created by liucxu on 2017/1/20.
 */

public class PersonInfoPresenterImpl implements IPersonInfoPresenter {

    private IPersonInfoView personInfo_view;
    private IPersonInfoModel model;

    public PersonInfoPresenterImpl(IPersonInfoView view) {
        personInfo_view = view;
        model = new IPersonInfoModelImpl();
    }

    @Override
    public void getMyInfo() {
        personInfo_view.showLoading();
        model.getMyInfo().compose(TransformUtils.<PersonInfoBean>defaultSchedulers()).subscribe(new CommonSubscriber<PersonInfoBean>() {
            @Override
            public void onSuccess(PersonInfoBean response) {
                personInfo_view.hideLoading();
                personInfo_view.showPersonInfo(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                personInfo_view.hideLoading();
                personInfo_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void getOtherUserInfo(String uid) {
        model.getOtherUserInfo(uid).compose(TransformUtils.<PersonInfoBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                personInfo_view.showLoading();
            }
        }).subscribe(new CommonSubscriber<PersonInfoBean>() {
            @Override
            public void onSuccess(PersonInfoBean response) {
                personInfo_view.hideLoading();
                personInfo_view.showPersonInfo(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                personInfo_view.hideLoading();
                personInfo_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void refreshPushMsg() {
        model.refreshPushMsg().compose(TransformUtils.<SimplePushMessageBean>defaultSchedulers()).subscribe(new CommonSubscriber<SimplePushMessageBean>() {
            @Override
            public void onSuccess(SimplePushMessageBean response) {
                personInfo_view.refreshPushMessages(response);
            }

            @Override
            public void onFailure(BaseBean response) {

            }
        });
    }
}
